<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>资源管理</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="/layer/layer.js"></script>
</head>
<body>

<div id="app">
    <!--  页眉  -->
    <nav class="navbar navbar-inverse" style="border-radius: 0">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                        aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">小黑资源系统</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li ><a href="/index.html">首页</a></li>
                    <li v-for="d in userResources" v-if="d.pid==0">
                        <!--  “v-...” 指的是vue里面的东西，主要用于ajax的内容显示  -->
                        <a v-bind:href="d.url">{{d.name}}</a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right hidden-sm">
                    <li><a>欢迎您：{{userName}}</a></li>
                    <!--      "{{}}" 里面放       -->
                    <li><a @click="logout()" href="#">退出</a></li>

                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <h2>资源管理</h2>
                <a href="#" type="button" class="btn btn-success" @click="resourceAdd()">添加</a>
            </div>
        </div>
        <!--主体-->
        <div class="row">
            <div class="col-sm-12" style="margin-top: 20px">
                <div class="panel-group" role="tablist" aria-multiselectable="true">
                    <div class="panel panel-default" v-for="r in resources_">
                        <!--      父类包括子类，父类开头      -->
                        <div class="panel-heading" role="tab">
                            <h4 class="panel-title" style="line-height: 2">
                                <a role="button" class="collapsed" data-toggle="collapse"
                                   aria-expanded="false" aria-controls="{r.name}" :href="'#'+r.name">
                                    {{r.name}} {{r.url}}
                                    <p class="pull-right">
                                        <a href="#" @click="updeatById(r.id)"
                                           class="btn btn-warning btn-sm">修改
                                        </a>
                                        <a href="#" @click="deleteById(r.id)"
                                           class="btn btn-danger btn-sm">
                                            删除
                                        </a>
                                    </p>

                                </a>
                            </h4>
                        </div>
                        <!--    子类开头      -->
                        <div :id="r.name" v-for="s in r.son" class="panel-collapse collapse"
                             aria-labelledby="heading${r.id}">
                            <ul class="panel-body">
                                <li class="list-group-item" style="line-height: 2">
                                    {{s.name}} {{s.url}}
                                    <p class="pull-right">
                                        <a href="#" @click="updeatById(s.id)"
                                           class="btn btn-warning btn-sm">
                                            修改
                                        </a>
                                        <a href="#" @click="deleteById(s.id)"
                                           class="btn btn-danger btn-sm">
                                            删除
                                        </a>
                                    </p>
                                </li>

                            </ul>
                        </div>
                        <!--    子类结束    -->
                    </div>
                    <!--   父类结束   -->
                </div>
            </div>
        </div>
    </div>

</div>


<script>
    new Vue({
        el: "#app",
        data: {
            userResources: [],
            resources_: [],
            userName: ''
        },
        created() {   //创建
            this.getUserInfo();
            this.getResource();
        },
        methods: {
            //对象显示
            getUserInfo() {
                var vm = this;
                $.post("/userInfo", {}, function (response) {
                    if (response.code == 200) {
                        vm.userName = response.data.name
                        vm.userResources = response.data.role.resourceList
                    } else {
                        location.href = "/login.html"
                    }
                })
            },
            //查询所有资源
            getResource() {
                var vm = this;
                $.post("/resource/resourceInfo", {}, function (response) {
                    if (response.code == 200) {
                        vm.resources_ = response.data
                    }
                })
            },
            //添加
            resourceAdd() {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '添加资源',
                    shadeClose: false,  //阴影
                    shade: 0.5,  //透明度
                    area: ['600px', '100%'],
                    content: 'add.html', //iframe的url
                    end: function () {
                        vm.getResource();
                    }
                });
            },
            //删除
            deleteById(id){
                var vm = this;
                layer.confirm('您确定要删除该资源？', {
                    btn: ['删除', '取消'] //按钮
                }, function () {
                    $.post("/resource/deleteId", {id: id}, function (response) {
                        if (response.code == 200) {
                            layer.msg('删除成功！', {
                                time: 1000,
                                end: function () {
                                    vm.getResource();
                                }
                            })
                        }
                    })
                }, function () {
                });
            },
            //修改
            updeatById(id){
                var vm = this;
                layer.open({
                    type: 2,
                    title: '角色修改',
                    shadeClose: false,  //阴影
                    shade: 0.5,  //透明度
                    area: ['600px', '100%'],
                    content: 'update.html?id=' + id,//iframe的url
                    end: function () {
                        vm.getResource();
                    }
                });
            },
            //退出
            logout() {
                $.post("/logout", {}, function (response) {
                    // location.href = "login.html"
                    layer.msg('退出成功！', {
                        time: 500,
                        end: function () {
                            location.href = "/login.html"
                        }
                    })
                })
            }

        }
    })
</script>
</body>
</html>